#!/bin/sh

RETRY_LIMIT=0
LOCAL_BIN_SHELL_UTILS_PATH="$PROJECT_PATH/src/main/resources/templates/shell-utils"
CLUSTER_NAMESPACE=test
CLUSTER_NAME=test
SERVICE_ACCOUNT=test
POD_NAME=test
BACKUP_CRD_NAME=sgbackups.stackgres.io
BACKUP_NAME=test
CLUSTER_CRD_NAME=sgclusters.stackgres.io
BACKUP_CONFIG_CRD_NAME=sgbackupconfigs.stackgres.io
CRONJOB_NAME=test
BACKUP_PHASE_RUNNING=Running
BACKUP_PHASE_COMPLETED=Completed
BACKUP_PHASE_FAILED=Failed
SCHEDULED_BACKUP_KEY=scheduled-backup
RIGHT_VALUE=true
JOB_NAME_KEY=job-name
JOB_NAME=test
BACKUP_CONFIG=test
CLUSTER_LABELS=app=StackGresCluster,cluster-uid=test,cluster-namespace=test,cluster-name=test
PATRONI_CONTAINER_NAME=patroni
PATRONI_ROLE_KEY=role
PATRONI_PRIMARY_ROLE=master
PATRONI_REPLICA_ROLE=replica
BACKUP_ENV=backup
LOCK_RESOURCE=sgclusters.stackgres.io
LOCK_RESOURCE_NAME=test
LOCK_SLEEP=0
LOCK_TIMEOUT_KEY=stackgres.io/lockTimeout
LOCK_POD_KEY=stackgres.io/lockPod
VOLUME_SNAPSHOT_CRD_NAME=volumesnapshot
PG_DATA_PATH=/var/lib/postgresql/data
POSTGRES_VERSION=17.2

shell_unit_test_backup() {
  . "$PROJECT_PATH/src/main/resources/templates/create-backup.sh"
}

mock kubectl kubectl_mock

kubectl_mock() {
  mokked
  case "$*" in
    ("get sgclusters.stackgres.io "*" --template="'
  LOCK_POD='*'
  LOCK_TIMEOUT='*'
  RESOURCE_VERSION={{ .metadata.resourceVersion }}
  ')
    echo '
      LOCK_POD=test
      LOCK_TIMEOUT='"$(( $(date +%s) + 1 ))"'
      RESOURCE_VERSION=test
      '
    ;;
    ("annotate sgclusters.stackgres.io "*)
    ;;
    ("get $CLUSTER_CRD_NAME -n $CLUSTER_NAMESPACE $CLUSTER_NAME --template={{ with .status }}{{ with .backupPaths }}{{ with (index . 0) }}{{ . }}{{ end }}{{ end }}{{ end }}")
    echo test
    ;;
    ("get $BACKUP_CONFIG_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_CONFIG --template={{ .metadata.resourceVersion }}")
    echo test
    ;;
    ("get $BACKUP_CRD_NAME -n $CLUSTER_NAMESPACE --template=$BACKUP_CR_TEMPLATE")
    ;;
    ("get $BACKUP_CONFIG_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_CONFIG -o json")
    printf %s '{}'
    ;;
    ("get $BACKUP_CRD_NAME -A --template=$BACKUP_CR_TEMPLATE")
    ;;
    ("get $BACKUP_CONFIG_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_CONFIG -o json")
    cat << EOF
{"spec":{"storage":{"type":"s3Compatible","s3Compatible":{"awsCredentials":{"secretKeySelectors":{"accessKeyId":{"key":"accesskey","name":"minio"},"secretAccessKey":{"key":"secretkey","name":"minio"}}},"endpoint":"http://minio.stackgres:9000","enablePathStyleAddressing":true,"bucket":"stackgres","region":"k8s"}}}}
EOF
    ;;
    ("get $BACKUP_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_NAME -o name")
    return 1
    ;;
    ("create -f /tmp/backup-to-create -o json")
    printf %s '{"metadata":{"uid":"test"}}'
    ;;
    ("get $BACKUP_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_NAME --template={{ .status.sgBackupConfig.storage }}")
    echo test
    ;;
    ("get pod -n $CLUSTER_NAMESPACE -l ${CLUSTER_LABELS},${PATRONI_ROLE_KEY}=${PATRONI_PRIMARY_ROLE} --template "*)
    echo test-0
    ;;
    ("get pod -n $CLUSTER_NAMESPACE -l ${CLUSTER_LABELS},${PATRONI_ROLE_KEY}=${PATRONI_REPLICA_ROLE} --template "*)
    echo test-1
    ;;
    ("get pod -n $CLUSTER_NAMESPACE test-0 --template "*)
    echo test-0
    ;;
    ("patch $BACKUP_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_NAME --type json --patch "*)
    ;;
    ("get pod -n $CLUSTER_NAMESPACE --template="*)
    ;;
    ("get $BACKUP_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_NAME --template={{ .status.backupPath }}")
    echo test
    ;;
    ("exec -i -n $CLUSTER_NAMESPACE test-0 -c $PATRONI_CONTAINER_NAME -- sh -e"*)
    STDIN="$(cat)"
    case "$STDIN" in
      (*" wal-g backup-push "*)
      cat << EOF
 Wrote backup with name test
EOF
      ;;
      ("pg_controldata "*)
      cat "$TEST_SHELL_PATH/backup/pg_controldata"
      ;;
    esac
    ;;
    ("exec -i -n $CLUSTER_NAMESPACE test-1 -c $PATRONI_CONTAINER_NAME -- sh -e"*)
    STDIN="$(cat)"
    case "$STDIN" in
      (*" for each existing backup"*)
      ;;
      (*" wal-g backup-list "*)
      cat "$TEST_SHELL_PATH/backup/wal-g-backup-list"
      ;;
    esac
    ;;
    ("exec -i -n $CLUSTER_NAMESPACE test-0 -c $PATRONI_CONTAINER_NAME -- psql -v ON_ERROR_STOP=1 -t -A")
    while read -r LINE
    do
      case "$LINE" in
        ("SELECT 'Backup bootstrap';")
        echo 'Backup bootstrap'
        ;;
        ("SELECT 'Backup started';")
        echo 'SET'
        echo '7328135472075981416|160001|f|0000000100000000000000C2|50331688'
        echo 'Backup started'
        ;;
        ("SELECT 'Backup stopped';")
        echo 'SET'
        echo '50420392|U1RBUlQgV0FMIExPQ0FUSU9OOiAwLzMwMDAwMjggKGZpbGUgMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAzKQpDSEVDS1BPSU5UIExPQ0FUSU9OOiAwLzMwMDAwNjAKQkFDS1VQIE1FVEhPRDogc3RyZWFtZWQKQkFDS1VQIEZST006IHByaW1hcnkKU1RBUlQgVElNRTogMjAyNC0wMS0yNSAyMDoyOToyOSBVVEMKTEFCRUw6IHRlc3QKU1RBUlQgVElNRUxJTkU6IDEK|'
        echo 'Backup stopped'
        ;;
      esac
    done
    ;;
    ("exec -n $CLUSTER_NAMESPACE $(cat /tmp/current-primary) -c patroni -- rm -rf "*)
    ;;
    ("create -f /tmp/snapshot-to-create")
    ;;
    ("get $VOLUME_SNAPSHOT_CRD_NAME -n $CLUSTER_NAMESPACE $BACKUP_NAME -o json")
    printf '%s\n' '{"status":{"readyToUse":"true","restoreSize":"1Gi"}}'
    ;;
    *)
    not_mokked
    ;;
  esac
}
